/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.web.interceptors;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.utils.Constants;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author 科泰集团教研团队
 * @version 1.0.0
 * @packageName com.ktjiaoyu.web.interceptors
 * @fileName AuthInterceptor.java
 * @createTime 2024/10/18 10:43
 * @lastModify 2024/10/18 10:43
 */
public class AuthInterceptor implements HandlerInterceptor {
    private static final Logger LOGGER =
            LoggerFactory.getLogger(AuthInterceptor.class);
    /** 前置处理方法,进入控制器之前经过此方法判断是否能够继续请求 */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LOGGER.debug("进入自定义拦截器AuthInterceptor，请求路径:{}", request.getRequestURI());
        HttpSession session = request.getSession();
        // 获取登录用户
        SysUser sysUser = (SysUser)session.getAttribute(Constants.SESSION_USER);
        // 验证登录状态
        if(null == sysUser){
            // 跳转到401错误页
            request.getRequestDispatcher("/WEB-INF/jsp/401.jsp").forward(request, response);
            // 返回false表示请求被拒绝，在返回之前需要做其他响应操作。
            return false;
        }
        // 返回true表示请求可以通过
        return true;
    }
}
